<template>
  <div>
    <el-container>
      <el-header style="height: 110px">
        <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 18px">
          <el-breadcrumb-item :to="{ path: '/'}" style="font-weight: bold;color: black"><i class="el-icon-s-promotion" style="margin-right: 5px"></i>后台管理</el-breadcrumb-item>
          <el-breadcrumb-item :to="{ path: '/sys-admin/permission/admin' }" style="font-weight: bold;color: black"><i class="el-icon-s-promotion" style="margin-right: 5px"></i>管理员管理</el-breadcrumb-item>
          <el-breadcrumb-item>添加管理员</el-breadcrumb-item>
        </el-breadcrumb>
        <el-divider></el-divider>
      </el-header>
      <el-main>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password"></el-input>
          </el-form-item>
          <el-form-item label="昵称" prop="nickname">
            <el-input v-model="ruleForm.nickname"></el-input>
          </el-form-item>
          <el-form-item label="头像" prop="avatar">
            <el-input v-model="ruleForm.avatar"></el-input>
          </el-form-item>
          <el-form-item label="电话" prop="phone">
            <el-input v-model="ruleForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="ruleForm.email"></el-input>
          </el-form-item>
          <el-form-item label="描述" prop="description">
            <el-input v-model="ruleForm.description"></el-input>
          </el-form-item>
          <el-form-item label="启用" prop="enable">
            <el-switch
                v-model="ruleForm.enable"
                active-value="1"
                inactive-value="0"
                active-color="#13ce66"
                inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
          <el-form-item label="角色" prop="roleIds">
            <el-select v-model="ruleForm.roleIds" multiple placeholder="请选择">
              <el-option
                  v-for="role in roles"
                  :key="role.id"
                  :label="role.name"
                  :value="role.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
            <el-button type="warning" @click="$router.push('/sys-admin/product/album')">返回列表</el-button>
          </el-form-item>
        </el-form>
      </el-main>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: '测试相册001',
        password: '测试相册的简介001',
        nickname: '测试相册的简介001',
        avatar: '99',
        phone: '99',
        email: '99',
        description: '99',
        enable: '0',
        roleIds: [],
      },
      rules: {
        username: [
          {required: true, message: '请输入名称', trigger: 'blur'},
          {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入简介', trigger: 'blur'}
        ]
      },
      roles: [],
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "http://localhost:9191/admin/add-new";
          console.log('url=' + url);

          let formData = this.qs.stringify(this.ruleForm);
          console.log(formData);

          let localJwt = localStorage.getItem("localJwt");
          this.axios.create({
            'headers': {
              'Authorization': localJwt
            }
          }).post(url, formData).then((response) => {
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              this.$message({
                message: '添加成功',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message.error(jsonResult.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loadRoleListItem() {
      let url = "http://localhost:9191/role/list";

      let localJwt = localStorage.getItem("localJwt");

      this.axios.create({
        'headers': {
          'Authorization': localJwt
        }
      }).get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.roles = jsonResult.data;
        } else {
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    }
  },
  mounted() {
    this.loadRoleListItem();
  }
}
</script>

